package com.beiming.libra.service.impl;

import com.beiming.libra.common.AppException;
import com.beiming.libra.common.enums.CaseUserTypeEnum;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.enums.StatusEnum;
import com.beiming.libra.common.utils.BeanConvertUtils;
import com.beiming.libra.common.utils.CommonsUtils;
import com.beiming.libra.dao.ArbitrationCaseAnswerMapper;
import com.beiming.libra.dao.ArbitrationCaseProblemMapper;
import com.beiming.libra.domain.ArbitrationCaseAnswer;
import com.beiming.libra.domain.ArbitrationCaseProblem;
import com.beiming.libra.domain.ArbitrationCaseUser;
import com.beiming.libra.domain.dto.requestdto.AnswerRequestDTO;
import com.beiming.libra.domain.dto.responsedto.AnswerResponseDTO;
import com.beiming.libra.service.ArbitrationCaseAnswerService;
import com.beiming.libra.service.ArbitrationCaseUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author zhengyu
 * @title
 * @create_time 2018/6/7
 */
@Service
@Slf4j
public class ArbitrationCaseAnswerServiceImpl implements ArbitrationCaseAnswerService {

  @Autowired
  private ArbitrationCaseAnswerMapper arbitrationCaseAnswerMapper;
  @Autowired
  private ArbitrationCaseUserService arbitrationCaseUserService;
  @Autowired
  private ArbitrationCaseProblemMapper arbitrationCaseProblemMapper;

  /**
   * 问题单答案获取服务
   *
   * @param problemId 案件申请书id
   * @param  userId 回答人id
   */
  @Override public List<ArbitrationCaseAnswer> listArbitrationCaseAnswers(String problemId, String userId) {
    ArbitrationCaseAnswer param = new ArbitrationCaseAnswer();
    param.setProblemId(problemId);
    param.setUserId(userId);
    //查询正在使用的数据
    param.setStatus(StatusEnum.USED.getCode());

    List<ArbitrationCaseAnswer> arbitrationCaseAnswers = arbitrationCaseAnswerMapper.select(param);

    return arbitrationCaseAnswers;
  }

  /**
   * 问题单答案精确获取服务
   *
   * @param id id
   */
  @Override public ArbitrationCaseAnswer getArbitrationCaseAnswer(String id) {
    return arbitrationCaseAnswerMapper.selectByPrimaryKey(id);
  }

  /**
   * 保存问题单答案
   *
   * @param arbitrationCaseAnswer 新问题单答案
   */
  @Override public void saveArbitrationCaseAnswer(ArbitrationCaseAnswer arbitrationCaseAnswer) {
    if (StringUtils.isEmpty(arbitrationCaseAnswer.getUserId())) {
      log.error("参数userid为空");
      throw new NullPointerException("参数不能为空");
    }
    arbitrationCaseAnswer.setId(CommonsUtils.get32BitUUID());
    arbitrationCaseAnswer.setStatus(StatusEnum.USED.getCode());

    arbitrationCaseAnswerMapper.insert(arbitrationCaseAnswer);
  }

  /**
   * 修改问题单答案
   *
   * @param arbitrationCaseAnswer 问题单答案
   */
  @Override public void updateArbitrationCaseAnswer(ArbitrationCaseAnswer arbitrationCaseAnswer) {
    arbitrationCaseAnswerMapper.updateByPrimaryKey(arbitrationCaseAnswer);
  }

  /**
   * 删除问题单答案
   *
   * @param id 问题单答案id
   */
  @Override public void removeArbitrationCaseAnswer(String id) {
    if (StringUtils.isEmpty(id)) {
      throw new NullPointerException("参数不能为空");
    }
    ArbitrationCaseAnswer arbitrationCaseAnswer = arbitrationCaseAnswerMapper.selectByPrimaryKey(id);

    if (null == arbitrationCaseAnswer) {
      throw new NullPointerException("删除的信息不存在");
    }
    arbitrationCaseAnswerMapper.deleteByPrimaryKey(id);
  }

  /**
   * 批量保存或修改问题单答案
   *
   * @param arbitrationCaseAnswers 问题单答案数组
   */
  @Override public void saveArbitrationCaseAnswers( List<ArbitrationCaseAnswer> arbitrationCaseAnswers) {
    for (ArbitrationCaseAnswer arbitrationCaseAnswer : arbitrationCaseAnswers) {
      if (StringUtils.isEmpty(arbitrationCaseAnswer.getId())) {
        saveArbitrationCaseAnswer(arbitrationCaseAnswer);
      } else {
        updateArbitrationCaseAnswer(arbitrationCaseAnswer);
      }
    }
  }

  /**
   * 回答问题
   *
   * @param answerRequestDTO
   */
  @Override public AnswerResponseDTO answerProblem(AnswerRequestDTO answerRequestDTO) {
    ArbitrationCaseAnswer arbitrationCaseAnswer = new ArbitrationCaseAnswer();
    arbitrationCaseAnswer.setUserId(answerRequestDTO.getUserId());
    arbitrationCaseAnswer.setProblemId(answerRequestDTO.getProblemId());
    arbitrationCaseAnswer.setAnswer(answerRequestDTO.getAnswer());
    arbitrationCaseAnswer.setUserName(answerRequestDTO.getUserName());
    ArbitrationCaseProblem arbitrationCaseProblem = arbitrationCaseProblemMapper.selectByPrimaryKey(
        answerRequestDTO.getProblemId());
    ArbitrationCaseUser param = new ArbitrationCaseUser();
    param.setCaseId(arbitrationCaseProblem.getCaseId());
    param.setUserId(answerRequestDTO.getUserId());
    List<ArbitrationCaseUser> arbitrationCaseUsers = arbitrationCaseUserService.select(param);
    if (!arbitrationCaseUsers.isEmpty()) {
      arbitrationCaseAnswer.setUserType(arbitrationCaseUsers.get(0).getUserType());
      if (arbitrationCaseAnswer.getUserType().equals(CaseUserTypeEnum.AGENT_APPLICANT.getCode())) {
        arbitrationCaseAnswer.setUserTypeName(CaseUserTypeEnum.AGENT_APPLICANT.getName());
      } else if (arbitrationCaseAnswer.getUserType().equals(CaseUserTypeEnum.APPLICANT.getCode())) {
        arbitrationCaseAnswer.setUserTypeName(CaseUserTypeEnum.APPLICANT.getName());
      } else if (arbitrationCaseAnswer.getUserType().equals(CaseUserTypeEnum.RESPONDENT.getCode())) {
        arbitrationCaseAnswer.setUserTypeName(CaseUserTypeEnum.RESPONDENT.getName());
      } else if (arbitrationCaseAnswer.getUserType().equals(CaseUserTypeEnum.AGENT_RESPONDENT.getCode())) {
        arbitrationCaseAnswer.setUserTypeName(CaseUserTypeEnum.AGENT_RESPONDENT.getName());
      }
    } else {
      throw new AppException(ErrorCode.USER_NOT_EXISTS, "该人员不是案件当事人或者代理人,不能回答");
    }
    saveArbitrationCaseAnswer(arbitrationCaseAnswer);
    AnswerResponseDTO  answerResponseDTO = BeanConvertUtils.copyBean(arbitrationCaseAnswer, AnswerResponseDTO.class);
    answerResponseDTO.setCreateTime(arbitrationCaseAnswer.getCreateTime());
    return answerResponseDTO;
  }
}
